//
//  KSHeaderMenu.swift
//  ZYA
//
//  Created by 邱仙凯 on 2021/6/15.
//  Copyright © 2021 aowang. All rights reserved.
//   列表头部筛选器

import UIKit

class KSHeaderMenu: NSObject {

}
class KSCityDropdown:UIView,UITableViewDelegate,UITableViewDataSource {
    
    var tableView:UITableView!
    var tableView2:UITableView!
    var bg:UIView!
    var resultClosure:((String)->())?
    var cityArr = [String]()

    override init(frame: CGRect){
         super.init(frame: frame)
         setup()
    }
    func setup() {
        cityArr = cityDic[provinceArr[0]]!
        
        self.isUserInteractionEnabled = true
        let gr = UITapGestureRecognizer(target: self, action: #selector(close))
        gr.numberOfTapsRequired = 1
        bg = UIView(frame: CGRect(x:0,y: 0 ,width: KSGlobal.screenW,height: 0))
        bg.isUserInteractionEnabled = true
        bg.backgroundColor = #colorLiteral(red: 0.6000000238, green: 0.6000000238, blue: 0.6000000238, alpha: 1).withAlphaComponent(0.7)
        addSubview(bg)
        bg.addGestureRecognizer(gr)
        
        tableView = UITableView(frame: CGRect(x:0,y:0,width: 100,height: 0))
        
        tableView.register(UITableViewCell.self, forCellReuseIdentifier: UITableViewCell.identifier)
        tableView.backgroundColor = .white
        tableView.alpha = 1
        tableView.delegate = self
        tableView.dataSource = self
        tableView.tableFooterView = UIView()
        tableView.isUserInteractionEnabled = true
        
        addSubview(tableView)
        
        tableView2 = UITableView(frame: CGRect(x:100,y:0,width: KSGlobal.screenW - 100,height: 0))
        
        tableView2.register(UITableViewCell.self, forCellReuseIdentifier: UITableViewCell.identifier)
        tableView2.backgroundColor = .white
        tableView2.alpha = 1
        tableView2.delegate = self
        tableView2.dataSource = self
        tableView2.tableFooterView = UIView()
        tableView2.isUserInteractionEnabled = true
        
        addSubview(tableView2)
    }
  
    override func willMove(toSuperview newSuperview: UIView?) {
       
        if newSuperview.isSome {
            UIView.animate(withDuration: 0.2) { //  出现时
                self.bg.ks_height = KSGlobal.screenH
                self.tableView.ks_height = 280
                self.tableView2.ks_height = 280

            }
        }
        
    }
 
    @objc func close(){
        UIView.animate(withDuration: 0.2) {
            self.bg.ks_height = 0
            self.tableView.ks_height = 0
            self.tableView2.ks_height = 0

        }
        
        DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 0.2) {
            self.removeFromSuperview()
        }
        
        
    }
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        if tableView == self.tableView {
            return provinceArr.count
        } else {
            return cityArr.count
        }
    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: UITableViewCell.identifier, for: indexPath)
        if tableView == self.tableView {
            cell.textLabel?.text = provinceArr[indexPath.row]
        } else {
            cell.textLabel?.text = self.cityArr[indexPath.row]
        }
       
        return cell
    }
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: true)
        
       
        
        if tableView == self.tableView {
            cityArr = cityDic[provinceArr[indexPath.row]]!
            self.tableView2.reloadData()
        } else {
            if let clo = resultClosure {
                clo(self.cityArr[indexPath.row])
            }
            self.close()
            
        }
        
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}

class KSDropdownView:UIView,UITableViewDelegate,UITableViewDataSource {
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private var tableView:UITableView!
    private var bg:UIView!
    var resultClosure:((String)->())?
    var arr = [String]()
    // 现在展示的是谁的数据源
    var nowIndex:Int? 
    init(frame: CGRect,arr:[String]){
        self.arr = arr
        super.init(frame: frame)
        setup()
    }
    func refreshDataSource(nowIndex:Int,arr:[String],resultClosure:@escaping  ((String)->())) {
        self.nowIndex = nowIndex
        self.arr = arr
        self.resultClosure = resultClosure
        self.tableView.reloadData()
    }
    
    func setup() {
        self.isUserInteractionEnabled = true
        let gr = UITapGestureRecognizer(target: self, action: #selector(close))
        gr.numberOfTapsRequired = 1
        bg = UIView(frame: CGRect(x:0,y: 0 ,width: KSGlobal.screenW,height: 0))
        bg.isUserInteractionEnabled = true
        bg.backgroundColor = #colorLiteral(red: 0.6000000238, green: 0.6000000238, blue: 0.6000000238, alpha: 1).withAlphaComponent(0.7)
        addSubview(bg)
        bg.addGestureRecognizer(gr)
        
        tableView = UITableView(frame: CGRect(x:0,y:0,width: KSGlobal.screenW,height: 0))
        
        tableView.register(UITableViewCell.self, forCellReuseIdentifier: UITableViewCell.identifier)
        tableView.backgroundColor = .white
        tableView.alpha = 1
        tableView.delegate = self
        tableView.dataSource = self
        tableView.tableFooterView = UIView()
        tableView.isUserInteractionEnabled = true
        
        addSubview(tableView)
    }
  
    override func willMove(toSuperview newSuperview: UIView?) {
       
        if newSuperview.isSome {
            self.bg.ks_height = KSGlobal.screenH
            UIView.animate(withDuration: 0.2) { //  出现时
                 self.tableView.ks_height = 280
            }
        }
        
    }
 
    @objc func close(){
        self.nowIndex = nil
        self.bg.ks_height = 0
        UIView.animate(withDuration: 0.2) {
             self.tableView.ks_height = 0
        }
        
        DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 0.2) {
            self.removeFromSuperview()
        }
        
        
    }
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.arr.count
    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: UITableViewCell.identifier, for: indexPath)
        cell.textLabel?.text = self.arr[indexPath.row]
        return cell
    }
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: true)
        self.close()
        if let clo = resultClosure {
            clo(self.arr[indexPath.row])
        }
        
    }
  
    
}
class KSHeaderItemView:UIView {
    
    var lab:UILabel!
    var img:UIImageView!
    
    func setTitle(_ str:String) {
        lab.text = str
    }
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.isUserInteractionEnabled = true
        lab = UILabel()
        img = UIImageView(image: UIImage(named:"ks_filter_down"))
        self.addSubview(lab)
        lab.textColor =  .darkGray
        lab.font = UIFont.systemFont(ofSize: 13)
        
        self.addSubview(img)
        lab.snp.makeConstraints { (make) in
            make.centerY.equalTo(self)
            make.centerX.equalTo(self).offset(-5)
  
        }
        img.snp.makeConstraints { (make) in
            make.centerY.equalTo(self)
            make.width.equalTo(10)
            make.height.equalTo(7)
            make.left.equalTo(lab.snp.right).offset(1)
        }
      
    }
   
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    override func layoutSubviews() {
        super.layoutSubviews()
       
        
    }
    
}
